/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.procurement.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.javaeye.common.util.DateUtils;
import com.javaeye.common.util.NumberUtils;
import com.javaeye.procurement.dto.SupplierOrder;
import com.javaeye.procurement.dto.SupplierOrderDetail;
import com.javaeye.procurement.service.IBackSupplierOutStoreService;
import com.javaeye.store.dto.Materials;
import com.javaeye.store.dto.MaterialsBatchDetail;
import com.javaeye.store.dto.MaterialsField;
import com.javaeye.store.dto.MaterialsFieldCustom;
import com.javaeye.store.dto.MaterialsSKUDetail;
import com.javaeye.store.service.IMaterialsBatchInfoService;

public class BackSupplierMaterialsAction extends SupplierOrderAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7408989332825417843L;
	
	private List<SupplierOrderDetail> materials;
	
	private List<String> outStroeBatchNos;
	
	private List<Float> outStroeNumbers;
	
	/**
	 * 物流需求ID
	 */
	private List<Integer> requestIds;
	
	private IMaterialsBatchInfoService materialsBatchInfoService;
	
	private IBackSupplierOutStoreService backSupplierOutStoreService;
	
	public BackSupplierMaterialsAction() {
		getCondition().setOrderType(SupplierOrder.ORDER_TYPE_BACK_SUPPLIER);
	}

	public void setBackSupplierOutStoreService(
			IBackSupplierOutStoreService backSupplierOutStoreService) {
		this.backSupplierOutStoreService = backSupplierOutStoreService;
	}

	public void setMaterialsBatchInfoService(
			IMaterialsBatchInfoService materialsBatchInfoService) {
		this.materialsBatchInfoService = materialsBatchInfoService;
	}
	
	private String getCustomName(String fileId, Set<MaterialsFieldCustom> fields) {
		for (MaterialsFieldCustom cf : fields) {
			if (cf.getFieldId().equals(fileId)) {
				return cf.getValue();
			}
		}
		return "";
	}
	
	private String getMaterialsSkuInfo(MaterialsSKUDetail mSku, Materials m) {
		List<MaterialsField> fields = mSku.getFields();
		StringBuffer sb = new StringBuffer();
		for (MaterialsField f : fields) {
			String fileId = f.getId();
			f.setCustomName(getCustomName(fileId, m.getCustomFields()));
			sb.append(f.getCustomName());
			sb.append(" ");
		}
		return sb.toString();
	}

	public String queryBackSupplierMaterialsList() throws Exception {
		List<MaterialsSKUDetail> materialSkus = materialsSKUDetailService.getNeedDefectiveMaterials();
		materials = new ArrayList<SupplierOrderDetail>();
		for (MaterialsSKUDetail mSku : materialSkus) {
			SupplierOrderDetail detail = new SupplierOrderDetail();
			Materials m = mSku.getMaterials();
			detail.setMaterialsId(mSku.getId());
			detail.setMaterialsRootCategory(m.getRootCategory().getName());
			detail.setMaterialsCategory(m.getCategory().getName());
			
			detail.setMaterialsName(m.getName());
			detail.setMaterialsType(m.getType());
			detail.setMaterialsSku(getMaterialsSkuInfo(mSku, m));
			detail.setUnit(m.getUnits());
			detail.setNumber(mSku.getDefectiveNumber());
			
			materials.add(detail);
		}
		return SUCCESS;
	}
	
	public String queryBackSupplierOrderForOutStoreList() throws Exception {
		getCondition().setStatus(new Integer[]{SupplierOrder.ORDER_STATES_CREATED});
		return querySupplierOrderList();
	}
	
	public String queryForNewBackOrder() throws Exception {
		SupplierOrder order = new SupplierOrder();
		setOrder(order);
		order.setOrderType(SupplierOrder.ORDER_TYPE_BACK_SUPPLIER);
		order.setCreateDate(DateUtils.now());
		
		double totleAmount = 0;
		for (int id : requestIds) {
			MaterialsSKUDetail mSku = materialsSKUDetailService.getMaterialsSKUDetail(id);
			int materialsId = mSku.getId();
			//奇怪的做法，和hibernate的延迟加载有关系
			//Materials m = materialsService.getMaterials(
			//		String.valueOf(mSku.getMaterials().getId()));
			Materials m = mSku.getMaterials();
			SupplierOrderDetail detail = new SupplierOrderDetail();
			detail.setMaterialsId(materialsId);
			detail.setMaterialsNo(m.getCode());
			detail.setMaterialsRootCategory(m.getRootCategory().getName());
			detail.setMaterialsCategory(m.getCategory().getName());
			
			detail.setMaterialsName(m.getName());
			detail.setMaterialsType(m.getType());
			detail.setMaterialsSku(getMaterialsSkuInfo(mSku, m));
			detail.setUnit(m.getUnits());
			detail.setNumber(mSku.getDefectiveNumber());
			detail.setPrice(materialsBatchInfoService.getMaxPrice(materialsId));
			detail.setAmount(NumberUtils.formatDouble(detail.getPrice() * detail.getNumber()));
			order.addDetail(detail);
			
			totleAmount += detail.getAmount();
		}
		order.setTotleAmount(totleAmount);
		return SUCCESS;
	}
	
	public String queryBackSupplierOrderForOutStore() throws Exception {
		querySupplierOrderInfo();
		SupplierOrder order = getOrder();
		Set<SupplierOrderDetail> orderDetails = order.getOrderDetails();
		for (SupplierOrderDetail detail : orderDetails) {
			float detailNumber = detail.getNumber();
			List<MaterialsBatchDetail> batchInfos = materialsBatchInfoService
					.getDefectiveMaterialsBatchInfo(detail.getMaterialsId());
			List<MaterialsBatchDetail> pageBatchInfos = new ArrayList<MaterialsBatchDetail>();
			for (MaterialsBatchDetail info : batchInfos) {
				float infoNumber = (float)NumberUtils.formatDouble(info.getPlanNumber() - info.getNumber());
				if (detailNumber > 0) {
					float outStoreNumber = (detailNumber < infoNumber) ? detailNumber : infoNumber;
					detailNumber -= outStoreNumber;
					MaterialsBatchDetail batchDetail = new MaterialsBatchDetail();
					batchDetail.setBatchNo(info.getBatchNo());
					batchDetail.setNumber(infoNumber);
					batchDetail.setOutNumber(outStoreNumber);
					pageBatchInfos.add(batchDetail);
				}
			}
			detail.setBatchInfos(pageBatchInfos);
		}
		return SUCCESS;
	}
	
	public String queryForEditBackOrder() throws Exception {
		querySupplierOrderInfo();
		return SUCCESS;
	}
	
	public String saveBackSupplierOrderOutStore() throws Exception {
		querySupplierOrderInfo();
		List<MaterialsBatchDetail> batchDetails = new ArrayList<MaterialsBatchDetail>();
		List<Integer> materialsIds = getMaterialsIds();
		for (int i = 0; i< materialsIds.size(); i++) {
			MaterialsBatchDetail detail = new MaterialsBatchDetail();
			detail.setMaterialsId(materialsIds.get(i));
			detail.setBatchNo(outStroeBatchNos.get(i));
			detail.setNumber(outStroeNumbers.get(i));
			batchDetails.add(detail);
		}
		backSupplierOutStoreService.saveOutStoreInfo(getOrder(), batchDetails);
		return SUCCESS;
	}

	public List<SupplierOrderDetail> getMaterials() {
		return materials;
	}

	public void setMaterials(List<SupplierOrderDetail> materials) {
		this.materials = materials;
	}

	public void setOutStroeBatchNos(List<String> outStroeBatchNos) {
		this.outStroeBatchNos = outStroeBatchNos;
	}

	public List<String> getOutStroeBatchNos() {
		return outStroeBatchNos;
	}

	public void setOutStroeNumbers(List<Float> outStroeNumbers) {
		this.outStroeNumbers = outStroeNumbers;
	}

	public List<Float> getOutStroeNumbers() {
		return outStroeNumbers;
	}

	public void setRequestIds(List<Integer> requestIds) {
		this.requestIds = requestIds;
	}

	public List<Integer> getRequestIds() {
		return requestIds;
	}
	
}
